/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.dejax.service.impl;

import com.dejax.domain.DmApptransaction;
import com.dejax.utils.ValidationUtil;
import com.dejax.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.dejax.repository.DmApptransactionRepository;
import com.dejax.service.DmApptransactionService;
import com.dejax.service.dto.DmApptransactionDto;
import com.dejax.service.dto.DmApptransactionQueryCriteria;
import com.dejax.service.mapstruct.DmApptransactionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.dejax.utils.PageUtil;
import com.dejax.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Richer
* @date 2022-01-06
**/
@Service
@RequiredArgsConstructor
public class DmApptransactionServiceImpl implements DmApptransactionService {

    private final DmApptransactionRepository dmApptransactionRepository;
    private final DmApptransactionMapper dmApptransactionMapper;

    @Override
    public Map<String,Object> queryAll(DmApptransactionQueryCriteria criteria, Pageable pageable){
        Page<DmApptransaction> page = dmApptransactionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmApptransactionMapper::toDto));
    }

    @Override
    public List<DmApptransactionDto> queryAll(DmApptransactionQueryCriteria criteria){
        return dmApptransactionMapper.toDto(dmApptransactionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmApptransactionDto findById(String transid) {
        DmApptransaction dmApptransaction = dmApptransactionRepository.findById(transid).orElseGet(DmApptransaction::new);
        ValidationUtil.isNull(dmApptransaction.getTransid(),"DmApptransaction","transid",transid);
        return dmApptransactionMapper.toDto(dmApptransaction);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmApptransactionDto create(DmApptransaction resources) {
        resources.setTransid(IdUtil.simpleUUID()); 
        return dmApptransactionMapper.toDto(dmApptransactionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmApptransaction resources) {
        DmApptransaction dmApptransaction = dmApptransactionRepository.findById(resources.getTransid()).orElseGet(DmApptransaction::new);
        ValidationUtil.isNull( dmApptransaction.getTransid(),"DmApptransaction","id",resources.getTransid());
        dmApptransaction.copy(resources);
        dmApptransactionRepository.save(dmApptransaction);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String transid : ids) {
            dmApptransactionRepository.deleteById(transid);
        }
    }

    @Override
    public void download(List<DmApptransactionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmApptransactionDto dmApptransaction : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("交易名称", dmApptransaction.getTransname());
            map.put("交易描述", dmApptransaction.getTransdesc());
            map.put("交易类型", dmApptransaction.getTrantype());
            map.put("所属应用系统", dmApptransaction.getAppid());
            map.put("应用系统代码标识", dmApptransaction.getAppcode());
            map.put("交易程序路径", dmApptransaction.getTransprogrampath());
            map.put("开发语言", dmApptransaction.getDevlanguage());
            map.put("开发人员", dmApptransaction.getDeveloper());
            map.put(" flag",  dmApptransaction.getFlag());
            map.put(" notes",  dmApptransaction.getNotes());
            map.put(" reguser",  dmApptransaction.getReguser());
            map.put(" regtime",  dmApptransaction.getRegtime());
            map.put(" moduser",  dmApptransaction.getModuser());
            map.put(" modtime",  dmApptransaction.getModtime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}